package com.systop.party08.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party08.R;
import com.systop.party08.adapter.FragPagerAdapter;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.HomeEntity;
import com.systop.party08.entity.TabVpEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.GeneralTools;
import com.systop.party08.utils.SPUtils;
import com.systop.party08.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.INDEX;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.home_tab)
    TabLayout homeTab;
    @BindView(R.id.home_vp)
    ViewPager homeVp;
    @BindView(R.id.home_refresh)
    SmartRefreshLayout refresh;

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

//                okhttpIndex();
                setView();
            }
        });
        refresh.autoRefresh();
        return view;
    }

    private void okhttpIndex() {
        String response="{\"msg\":\"请求成功\",\"status\":\"200\",\"data\":{\"lmList\":[{\"name\":\"政策文件\",\"id\":6},{\"name\":\"活动证管理\",\"id\":12}]}}";
        HashMap<String, String> param = new HashMap<>();
        HomeEntity entity = gson.fromJson(response, HomeEntity.class);
//        setView(entity);

    }

    private void setView() {

                ArrayList<TabVpEntity> mTabVps = new ArrayList<>();
                     mTabVps.add(new TabVpEntity("首页", TabOneFragment.newInstance("首页","")));

                    mTabVps.add(new TabVpEntity("政策文件", TabOneFragment.newInstance("政策文件","")));

                FragPagerAdapter adapter = new FragPagerAdapter(getActivity().getSupportFragmentManager(), mTabVps);
                homeVp.setAdapter(adapter);
                homeTab.setupWithViewPager(homeVp);
                homeVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                       showMessage("滚动"+position+"个Tab");
                    }

                    @Override
                    public void onPageSelected(int position) {
                        showMessage("选中"+position+"个Tab");
                        if (position == 0) {
                            refresh.setEnableLoadMore(false);
                            refresh.setEnableRefresh(true);
                        } else {
                            refresh.setEnableLoadMore(false);
                            refresh.setEnableRefresh(false);
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });


    }


}